
use std::ops::*;
use std::fmt::{self, Display};

use super::super::utils::*;

pub mod types {
    pub type Point3 = super::Vec3;
    pub type Color = super::Vec3;

    impl Color {
        pub fn white() -> Self {
            Color::new(1.,1.,1.)
        }

        pub fn black() -> Self {
            Self::default()
        }
    }
}

#[derive(Debug,Clone,Copy, Default)]
pub struct Vec3{
    pub e: [f32;3]
}


impl Vec3 {
    pub fn new(x:f32, y: f32, z: f32) -> Self { 
        Self {
            e: [x,y,z]
        } 
    }

    pub fn x(&self) -> f32 {
        self.e[0]
    }

    pub fn y(&self) -> f32 {
        self.e[1]
    }

    pub fn z(&self) -> f32 {
        self.e[2]
    }

    pub fn r(&self) -> f32 {
        self.e[0]
    }
    pub fn g(&self) -> f32 {
        self.e[1]
    }
    
    pub fn b(&self) -> f32 {
        self.e[2]
    }

    pub fn length(&self) -> f32 {
        f32::sqrt(self.length_squard())
    }

    pub fn length_squard(&self) -> f32 {
        self.e.iter().map(|v| f32::powf(*v, 2.0)).sum()
    }

    pub fn dot(&self, rhs: &Vec3) -> f32 {
        self.e[0] * rhs.e[0] +
        self.e[1] * rhs.e[1] +
        self.e[2] * rhs.e[2]
    }

    pub fn cross(&self, rhs: &Vec3) -> Vec3 {
        let x = self.e[1] * rhs.e[2] - self.e[2] * rhs.e[1];
        let y = self.e[2] * rhs.e[0] - self.e[0] * rhs.e[2];
        let z = self.e[0] * rhs.e[1] - self.e[1] * rhs.e[0];
        Vec3::new(x,y,z)
    }

    pub fn unit_vector(&self) -> Self {
        *self / self.length()
    }

    pub fn near_zero(&self) -> bool {
        let s:f32 = 1.0e-8;
        return (f32::abs(self.e[0]) < s) && (f32::abs(self.e[1]) < s) && (f32::abs(self.e[2]) < s);
    }
}

impl Display for Vec3 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {} {}", self.e[0], self.e[1], self.e[2])
    }
}

impl Add for Vec3 {
    type Output = Vec3;
    fn add(self, rhs: Self) -> Self::Output {
        let e = [
            self.e[0] + rhs.e[0],
            self.e[1] + rhs.e[1],
            self.e[2] + rhs.e[2],
        ];

        Self {e}
    }
}

impl AddAssign for Vec3 {
    fn add_assign(&mut self, rhs: Vec3) {
        self.e[0] += rhs.e[0];
        self.e[1] += rhs.e[1];
        self.e[2] += rhs.e[2];
    }
        
}

impl Sub for Vec3 {
    type Output = Vec3;
    fn sub(self, rhs: Vec3) -> Self::Output {
        let e = [
            self.e[0] - rhs.e[0],
            self.e[1] - rhs.e[1],
            self.e[2] - rhs.e[2],
        ];

        Self {e}
    }
}

impl SubAssign for Vec3 {
    fn sub_assign(&mut self, rhs: Vec3) {
        self.e[0] -= rhs.e[0];
        self.e[1] -= rhs.e[1];
        self.e[2] -= rhs.e[2];
    }
}

impl Mul for Vec3 {
    type Output = Vec3;
    fn mul(self, rhs: Vec3) -> Self::Output {
        let e = [
            self.e[0] * rhs.e[0],
            self.e[1] * rhs.e[1],
            self.e[2] * rhs.e[2],
        ];
        Self{e}
    }
}

impl Mul<Vec3> for f32 {
    type Output = Vec3;
    fn mul(self, v: Vec3) -> Self::Output {
        Vec3 {
            e: [
                self * v.e[0],
                self * v.e[1],
                self * v.e[2]
            ]
        }
    }
}

impl MulAssign for Vec3 {
    fn mul_assign(&mut self, rhs: Vec3) {
        self.e[0] *= rhs.e[0];
        self.e[1] *= rhs.e[1];
        self.e[2] *= rhs.e[2];
    }
}

impl MulAssign<f32> for Vec3 {
    fn mul_assign(&mut self, f: f32) {
        self.e[0] *= f;
        self.e[1] *= f;
        self.e[2] *= f;
    }
}

impl Div for Vec3 {
    type Output = Vec3;
    fn div(self, rhs: Vec3) -> Self::Output {
        let e = [
            self.e[0] / rhs.e[0],
            self.e[1] / rhs.e[1],
            self.e[2] / rhs.e[2],
        ];
        Self {e}
    }
}

impl Div<f32> for Vec3 {
    type Output = Vec3;
    fn div(self, f: f32) -> Self::Output {
        Vec3{
            e:[
                self.e[0] / f,
                self.e[1] / f,
                self.e[2] / f,
            ]
        }
    }
}

impl DivAssign for Vec3 {
    fn div_assign(&mut self, rhs: Vec3) {
        self.e[0] /= rhs.e[0];
        self.e[1] /= rhs.e[1];
        self.e[2] /= rhs.e[2];
    }
}

impl DivAssign<f32> for Vec3 {
    fn div_assign(&mut self, f:f32 ) {
        self.e[0] /= f;
        self.e[1] /= f;
        self.e[2] /= f;
    }
}

impl Neg for Vec3  {
    type Output = Vec3;
    fn neg(self) -> Self::Output {
        Self {
            e: [- self.e[0], -self.e[1], -self.e[2]]
        }
    }
}

impl Index<usize> for Vec3 {
    type Output = f32;
    fn index(&self, index: usize) -> &Self::Output {
        &self.e[index]
    }
}

impl IndexMut<usize> for  Vec3 {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.e[index]
    }
}


pub fn random() -> Vec3 {
    Vec3{
        e: [random_f32(),random_f32(),random_f32()]
    }
}

pub fn random_in_range(min: f32, max: f32) -> Vec3 {
    Vec3 {
        e: [random_f32_in_range(min, max),
            random_f32_in_range(min, max),
            random_f32_in_range(min, max)]
    }
}

pub fn random_in_unit_sphere() -> Vec3 {
    loop {
        let p =random_in_range(-1., 1.);
        if p.length_squard() > 1.0 {
           continue;
        } else {
            return p;
        }           
    }
}

pub fn random_unit_vector() -> Vec3 {
   random_in_unit_sphere().unit_vector()
}

pub fn random_in_hemisphere(normal: &Vec3) ->Vec3 {
    let in_unit_sphere =random_in_unit_sphere();
    if in_unit_sphere.dot(normal) > 0.0 {
        return in_unit_sphere;
    } else {
        return -in_unit_sphere;
    }
}


pub fn reflect(v: &Vec3, n: &Vec3) -> Vec3 {
    *v - 2.0 * v.dot(n) * *n
}


mod tests {

    use super::*;

    #[test]
    fn test_mul() {
        let v = Vec3::new(3.0, 4.0, 5.0);
        println!("{}, {}", v.length(), v.length_squard());
    }
}